//
//  UserAccount.swift
//  LX_weibo
//
//  Created by LTT❤️ on 15/6/28.
//  Copyright © 2015年 LTT. All rights reserved.
//

import UIKit

class UserAccount: NSObject ,NSCoding{
    //授权后的access token
    var access_token:String
    //生命周期
    var expires_in:NSTimeInterval
    
    var  expiresDate:NSDate
    //授权用户的UID
    var uid:String
    //用户友好显示名称
    var name:String?
    //头像地址
    var avatar_large:String?
    
    
    
    init(dict:[String:AnyObject]){
        access_token = dict["access_token"] as! String
        expires_in = dict["expires_in"] as! NSTimeInterval
        uid = dict["uid"] as! String
        //计算过期日期
        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
    }
    
    func loadUserInfo(finished:(account:UserAccount?,error:NSError?) ->()){
        //加载用户信息 
        let  params = ["access_token":access_token,"uid":uid]
        NetworkTools.sharedNetworkTools().GET("2/users/show.json", parameters: params, success: { (_, JSON) -> Void in
            let dict = JSON as![String:AnyObject]
            self.name = (dict["name"] as? String)!
            self.avatar_large = dict["avatar_large"] as? String
            self.saveAccount()
            //通知调用方 处理完成 
            
            finished(account: self, error: nil)
            
            
            print(self)
            }) { (_, error) -> Void in
                print(error)
                finished(account: nil, error: error)
        }
    }
    override var description : String {
        let properties = ["access_token","expires_in","uid","expiresDate","avatar_large","name"]
        
        return "\(self.dictionaryWithValuesForKeys(properties))"
    }
    
    
    
    //定义路径
   static let accountpath = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingPathComponent("account.plist")

    func saveAccount(){
        print(UserAccount.accountpath)
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.accountpath)
    }
    class func loadAccount() -> UserAccount?{
        
        //判断token是否已经过期进入登陆界面
       if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(accountpath)as?UserAccount{
        if account.expiresDate.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            return account
        }
        }
        
        return nil
    }
    

    
    
    //解档  将二进制数据转换成对象
    required init?(coder aDecoder:NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as!String
        //过期时间
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as! NSDate
        uid = aDecoder.decodeObjectForKey("uid")as!String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    //归档,将对象转换成二进制数据保存到磁盘
    func encodeWithCoder(aCoder:NSCoder){
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    
    
}
